package com.example.lance.textapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.KeyEvent;
import android.view.View;

public class MyView extends View implements Runnable {
    private final Paint mPaint;
    private int mICount = 0;
    private final TextUtil mTextUtil;

    private final TextInRectUtil textInRectUtil1;
    private final TextInRectUtil textInRectUtil2;
    private final TextInRectUtil textInRectUtil3;

    /**
     * @param context
     */
    public MyView(Context context) {
        super(context);
        mPaint = new Paint();
        /*String string = "测试自动换行(第1行)\n测试自动换行(第2行)\n测试自动换行(第3行)\n测试自动换行(第4行)\n测试自动换行(第5行)\n测试自动换行(第6行)\n测试自动换行(第7行)"
                + "生活教會了我們什麼，自己的感覺現在已經徹底的隕滅，生命中彷彿是蒸發了為自己幸福而爭的念頭，一切就這樣的循規蹈矩！";
        */
        String string = "测试自动换行\n测试自动换行(第2行)\n测试自动换行(第3行)\n";
        mTextUtil = new TextUtil(string, 400, 300, 800, 300, 0xff00ff, 255, 40, Paint.Align.CENTER);
        mTextUtil.InitText();
        new Thread(this).start();


        String message = "这\n是一个文本框的测试项目";

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setTextSize(20);

        Rect rect = new Rect(100, 500, 200, 800);

        textInRectUtil1 = new TextInRectUtil(message, rect, paint);


        Paint paint2 = new Paint();
        paint2.setColor(Color.RED);
        paint2.setAntiAlias(true);
        paint2.setTextAlign(Paint.Align.CENTER);
        paint2.setTextSize(40);

        Rect rect2 = new Rect(300, 500, 400, 800);

        textInRectUtil2 = new TextInRectUtil(message, rect2, paint2);


        Paint paint3 = new Paint();
        paint3.setColor(Color.YELLOW);
        paint3.setAntiAlias(true);
        paint3.setTextAlign(Paint.Align.RIGHT);
        paint3.setTextSize(20);

        Rect rect3 = new Rect(500, 500, 600, 800);

        textInRectUtil3 = new TextInRectUtil(message, rect3, paint3);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.LTGRAY);
        mPaint.setSubpixelText(true);
        if (mICount < 100) {
            mICount++;
        }
        mPaint.setColor(Color.RED);
        canvas.drawText("裝載進度：" + mICount + "%......", 10, 20, mPaint);
        mTextUtil.DrawText(canvas);

        textInRectUtil1.drawText(canvas);
        textInRectUtil2.drawText(canvas);
        textInRectUtil3.drawText(canvas);
    }

    @Override
    public void run() {
        Thread.currentThread();
        while (!Thread.interrupted()) {
            try {
                Thread.sleep(10);
            } catch (Exception ex) {
                ex.printStackTrace();
                Thread.currentThread().interrupt();
            }
            this.postInvalidate();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return mTextUtil.KeyDown(keyCode, event);
    }

}
